package org.go.work;

import org.go.Go;
import org.go.util.Key;
import org.go.work.self.NoOpWork;

public class WorkBuilder {
	private Class<? extends Go> jobClass = NoOpWork.class;
	private String description;
	private WorkKey workKey;

	/**
	 * Create a WorkBuilder with which to define a <code>Work</code>,
	 * and set the class name of the <code>Work</code> to be executed.
	 * 
	 * @return a new WorkBuilder
	 */
	public static WorkBuilder newWork(Class<? extends Go> jobClass) {
		WorkBuilder b = new WorkBuilder();
		b.ofType(jobClass);
		return b;
	}

	/**
	 * Set the class which will be instantiated and executed when a
	 * Trigger fires that is associated with this JobDetail.
	 * 
	 * @param jobClass a class implementing the Job interface.
	 * @return the updated JobBuilder
	 * @see JobDetail#getJobClass()
	 */
	public WorkBuilder ofType(Class<? extends Go> jobClass) {
		this.jobClass = jobClass;
		return this;
	}

	/**
	 * Produce the <code>JobDetail</code> instance defined by this 
	 * <code>JobBuilder</code>.
	 * 
	 * @return the defined JobDetail.
	 */
	public Work build() {
		Work work = new Work();
		try {
			work.setGo(jobClass.newInstance());
			work.setDescription(description);
			if (workKey == null) {
				workKey = new WorkKey(Key.createUniqueName(null), null);
			}
			work.setKey(workKey);
			//		job.setDurability(durability);
			//		job.setRequestsRecovery(shouldRecover);
			//
			//		if (!jobDataMap.isEmpty())
			//			job.setJobDataMap(jobDataMap);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return work;
	}

	public WorkBuilder withIdentity(String name, String group) {
		this.workKey = new WorkKey(name, group);
		return this;
	}
}
